package com.grace.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.grace.beans.LogType;
import com.grace.common.RequestHolder;
import com.grace.dao.SysLogMapper;
import com.grace.dao.SysRoleAclMapper;
import com.grace.dao.SysRoleMapper;
import com.grace.dao.SysRoleUserMapper;
import com.grace.model.SysLogWithBLOBs;
import com.grace.model.SysRole;
import com.grace.model.SysRoleAcl;
import com.grace.util.JsonMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleAclService {

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private SysLogMapper sysLogMapper;


    /**
     * 改变角色权限
     */
    public void changeRoleAcls(Integer roleId, List<Integer> aclIdList) {
        //根据查出来的角色id列表查权限id列表
        List<Integer> originAclIdList = sysRoleAclMapper.getAclIdListByRoleIdList(Lists.newArrayList(roleId));
        //判断是否相同, 使用Set来进行比对
        if (originAclIdList.size() == aclIdList.size()) {
            Set<Integer> originAclIdSet = Sets.newHashSet(originAclIdList);
            System.out.println(originAclIdSet);//[1, 2, 3, 5, 12, 14]
            Set<Integer> aclIdSet = Sets.newHashSet(aclIdList);
            System.out.println(aclIdSet);
            //移除之后，如果什么都不剩则完全一致
            originAclIdList.removeAll(aclIdSet);
            if (CollectionUtils.isEmpty(originAclIdList)) {
                return;
            }
        }
        //如果不相同, 则做更新操作
        updateRoleAcls(roleId, aclIdList);

        //记录日志
        saveRoleAclLog(roleId, originAclIdList, aclIdList);

    }

    /**
     * 更新角色权限
     */
    @Transactional
    public void updateRoleAcls(int roleId, List<Integer> aclIdList) {
        //先移除旧的,然后再添加重新添加
        sysRoleAclMapper.deleteByRoleId(roleId);
        if (CollectionUtils.isEmpty(aclIdList)) {
            return;
        }
        //将新的放在roleAclList中
        List<SysRoleAcl> roleAclList = Lists.newArrayList();
        for (Integer aclId : aclIdList) {
            SysRoleAcl roleAcl = SysRoleAcl.builder().roleId(roleId).aclId(aclId).build();
            roleAcl.setOperator(RequestHolder.getCurrentUser().getUsername());
            roleAcl.setOperatorTime(new Date());
            roleAclList.add(roleAcl);
        }
        //调用批量增加的功能增加到角色权限表中
        sysRoleAclMapper.batchInsert(roleAclList);

    }

    /**
     * 保存角色_权限的日志信息
     */
    public void saveRoleAclLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLog = new SysLogWithBLOBs();
        sysLog.setType(LogType.TYPE_ROLE_ACL);
        sysLog.setTargetId(roleId);
        sysLog.setOldValue(before == null ? "" : JsonMapper.obj2String(before));
        sysLog.setNewValue(after == null ? "" : JsonMapper.obj2String(after));
        sysLog.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysLog.setOperatorTime(new Date());
        sysLog.setStatus(1);
        sysLogMapper.insertSelective(sysLog);
    }


}
